package yy.robot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import yy.robot.domain.Paging;
import yy.robot.domain.RobotRunning;
import yy.robot.service.RobotRunningService;

/**
 * Created by BaiJiFeiLong@gmail.com at 2018/5/3 11:13
 */
@RestController
@RequestMapping("/robotRunnings")
public class RobotRunningController {

    private final RobotRunningService robotRunningService;

    @Autowired
    public RobotRunningController(RobotRunningService robotRunningService) {
        this.robotRunningService = robotRunningService;
    }

    @GetMapping
    public Paging<RobotRunning> index(
            @RequestParam(required = false, defaultValue = "0") int page,
            @RequestParam(required = false, defaultValue = "10") int size
    ) {
        Page<RobotRunning> robotRunningPage = robotRunningService.findAll(PageRequest.of(page, size));
        return Paging.fromPage(robotRunningPage);
    }

    @PostMapping
    public RobotRunning create(
            RobotRunning robotRunning
    ) {
        return robotRunningService.create(robotRunning);
    }

    @GetMapping("{id}")
    public RobotRunning show(@PathVariable long id) {
        return robotRunningService.findById(id);
    }

    @PatchMapping("{id}")
    public RobotRunning update(
            @PathVariable long id,
            RobotRunning robotRunning
    ) {
        robotRunning.setId(id);
        return robotRunningService.update(robotRunning);
    }

    @DeleteMapping("{id}")
    public void delete(@PathVariable long id) {
        robotRunningService.destroy(id);
    }
}
